<?php

class fop_FoplineController extends Zend_Controller_Action
{
  function init() {
  	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $this->getHelper('viewRenderer')->setNoRender();
  }

  function createAction() {
    require_once('fop/objects/fopline.php');
    try{
	  $results = fop_Object_Fopline::create($this->_getAllParams());
    } catch(fop_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
	$data['status'] = 	'OK';
	echo Zend_Registry::get('dt_serializer')->serialize($data);;
    return;
  }
  
  function getAction() {
    require_once('fop/objects/fopline.php');
    try {
      $fopline = new fop_Object_Fopline($this->_getParam('name'),$this->_getParam('tableName'));
      $resFopline = $fopline->asArray();
      $FOPTableLine = $fopline->render('FOPTable'); 
      $FOPRegularExpression = $fopline->render('RegularExpression'); 
    } catch(fop_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
    
    //We list the different actions of the controller
    //require_once('fop/objects/controller.php');
    //$resControllers = fop_Object_Controller::listing($module->getName());
    
	$data['status'] = 	'OK';
	$data['fopline'] = $resFopline;
	$data['FOPTableLine'] = $FOPTableLine;
	$data['FOPRegularExpression'] = $FOPRegularExpression;
	
	//We get also the last published version of the message
    require_once('fop/objects/message.php');
    try{
      $message = new fop_Object_Message($this->_getParam('name'),$this->_getParam('tableName'),'','current');
      $resMessage = $message->asArray();
    } catch (fop_Object_Exception $e) {
      $data['message']['status'] = 'KO';
      $data['message']['error']['num'] = 	$e->getCode();
      $data['message']['error']['message'] = 	$e->getMessage();      	
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return; 
    }
    if(isset($resMessage) && !empty($resMessage)) {
      $data['message'] = $resMessage;	
      $data['message']['status'] = 'OK';
    }
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;  	
  }
  
  function gethistoryAction() {
  	require_once('fop/objects/fopline.php');
    try {
      $fopline = new fop_Object_Fopline($this->_getParam('name'),$this->_getParam('tableName'));
      $resFopline = $fopline->asArray();
    } catch(fop_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
    
    //We list the different actions of the controller
    //require_once('fop/objects/controller.php');
    //$resControllers = fop_Object_Controller::listing($module->getName());
    
	$data['status'] = 	'OK';
	$data['fopline'] = $resFopline;
	
	//We get the ddata of history for the message
    try{
      $historyList = $fopline->getHistory();
    } catch (fop_Object_Exception $e) {
      $data['message']['status'] = 'KO';
      $data['message']['error']['num'] = 	$e->getCode();
      $data['message']['error']['message'] = 	$e->getMessage();      	
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return; 
    }
    if(isset($historyList) && !empty($historyList)) {
      $data['history'] = $historyList;	
    }
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;  	

  }
  
  
  
  
  
  
  function renderAction() {
  	require_once('fop/objects/fopline.php');
    try {
      $fopline = new fop_Object_Fopline($this->_getParam('name'),$this->_getParam('tableName'));
      $render = $fopline->render($this->_getParam('mode')); 
    } catch (fop_Object_Exception $e) {
      $data['message']['status'] = 'KO';
      $data['message']['error']['num'] = 	$e->getCode();
      $data['message']['error']['message'] = 	$e->getMessage();
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;      	
    }

	$data['status'] = 	'OK';
	$data['mode'] = $this->_getParam('mode');
	$data['fopline'] = $fopline->asArray();
	$data['render'] = $render;	
	
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;
    
  }






  function validatecrypticAction() {
  	require_once('fop/objects/fopline.php');
    try {
      $fopline = new fop_Object_Fopline($this->_getParam('foplineName'),$this->_getParam('tableName'));
      $validation = $fopline->matchCryptic($this->_getParam('crypticEntry'));
    } catch (fop_Object_Exception $e) {
      $data['message']['status'] = 'KO';
      $data['message']['error']['num'] = 	$e->getCode();
      $data['message']['error']['message'] = 	$e->getMessage();
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;      	
    }
    


	$data['status'] = 	'OK';
	$data['fopline'] = $fopline->asArray();
	$data['validated'] = $validation['ok'];

	if($validation['ok']) $data['matchingData'] = $validation['matchingData'];
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;
    
  }



} //End class


?>